package MenuUnit
{
	import BasicEngine.LoaderManager;
	import BasicEngine.MsgId;
	import BasicEngine.MsgManager;
	import DataStore.DataSource;
	import DataStore.ResourceManager;
	import flash.display.ShaderJob;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import Player.AnimationEntData;
	import Player.AnimationProc;
	import Player.AnimationUnit;
	import Player.DataCenter;
	import Player.VisualEntData;
	
	import GameScheduler.Game;
	
	import Map.MapManager;
	
	import Props.PropManager;
	
	import SceneUnit.PlayScene;
	import SceneUnit.SceneManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class LoginMenu extends Menu
	{
		//布局说明文件，下载那些图片，把图片放到什么位置，动画，都是由这个文件决定的。
		
		private var xmlLayoutBook:String = "../Resource/Image/Menu/loginMenu.xml";
		private var imgs:Vector.<String>;
		private var imgPoses:Vector.<Point>;	
		private var imgDepth:Vector.<int>;
		
		private var loading:Boolean = false;
		private var border:Sprite;
		private var grainBmp:String;	
		
		private var tempTest:Boolean;
		
		public function LoginMenu()
		{
			imgs = new Vector.<String>();
			imgPoses = new Vector.<Point>();
			imgDepth = new Vector.<int>();							
		}	   
		
		public override function Active():void
		{
			border = new Sprite();
			Add(border,0,0);
			border.graphics.beginFill(0x882E0C);
			border.graphics.drawRoundRect(105, 240, 410, 320, 10, 10);
			
			LoaderManager.xmlLoader.addEventListener(Event.COMPLETE, OnLoadXmlCompleted);
			LoaderManager.xmlLoader.load(new URLRequest(xmlLayoutBook));	
		}
		
		public function OnLoadXmlCompleted(e:Event):void
		{
			  var loader:URLLoader = e.target as URLLoader;
			  var xml:XML = new XML(loader.data);
			  LoaderManager.xmlLoader.removeEventListener(Event.COMPLETE, OnLoadXmlCompleted);		  
			  for each(var temp:XML in xml.grain)
			  {
				  grainBmp = temp.@url[0];
			  }
			  
			  for each(var el:XML in xml.img)
			  {
				  var url:String = (el.@url[0]);
				  imgs.push(url);
				  var x:int = parseInt(el.@x[0]);
				  var y:int = parseInt(el.@y[0]);
				  imgPoses.push(new Point(x, y));
				  var depth:int = parseInt(el.@depth[0]);
				  imgDepth.push(depth);
			  }
			  for each(var item:XML in xml.txt)
			  {
				  var x1:int = parseInt(item.@x[0]);
				  var y1:int = parseInt(item.@y[0]);
				  var fontSize:int = parseInt(item.@fontSize[0]);
				  var clr:int = parseInt(item.@clr[0]);
				  var st:String = item.@val[0];
				  var letterSp:int = parseInt(item.@lp[0]);
				  				  
				  var txt:TextField = new TextField();
				  txt.text = st;
				  txt.width = st.length * fontSize * 10;
				  txt.selectable = false;
				  txt.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void { AddMenuHandler(e); } );				  
				  
				  var txtFormat:TextFormat = new TextFormat();
				  txtFormat.size = fontSize;
				  txtFormat.color = clr;
				  txtFormat.letterSpacing = letterSp;
				  
				  txt.setTextFormat(txtFormat);
				  Add(txt, x1, y1);   
				  
			  }
			 StarLoadGrain();
		}
		
		public function StarLoadGrain():void
		{
			LoaderManager.imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnLoadGrain);
			var st:String = grainBmp;
			LoaderManager.imgLoader.load(new URLRequest(grainBmp));
		}
		
		public function OnLoadGrain(e:Event):void
		{
			var loader2:Loader = (e.target.loader) as Loader;			
			var bmpData:BitmapData = (loader2.content as Bitmap).bitmapData;
			MenuManager.RootScene.graphics.beginBitmapFill(bmpData);
			MenuManager.RootScene.graphics.drawRect(0, 0, 1200, 742);			
			LoaderManager.imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnLoadGrain);
			
			StarLoadImg();
		}
		
		public function StarLoadImg():void
		{
			if (!loading)
			{
				loading = true;
				LoaderManager.imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnLoadImgs);
				LoadStep();				
			}
		}
		
		public function LoadStep():void
		{
			if (imgs.length == 0)
			{
				loading = false;
				LoaderManager.imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnLoadImgs);		
				
				(SceneManager.MainScene as PlayScene).AddMap("征战天下");
				(SceneManager.MainScene as PlayScene).MoveMap(720, 220);
				(SceneManager.MainScene as PlayScene).FullGrain("16-0");
				
				return;
			}
			var task:String = imgs.shift();
			LoaderManager.imgLoader.load(new URLRequest(task));
		}
		
		private function OnLoadImgs(e:Event):void
		{
			var loader2:Loader = (e.target.loader) as Loader;			
			var bmpData:BitmapData = (loader2.content as Bitmap).bitmapData;
			var bitmap:Bitmap = new Bitmap();
			bitmap.bitmapData = bmpData;
			var pos:Point = imgPoses.shift();
			var depth:int = imgDepth.shift();
			Add(bitmap, pos.x, pos.y);
			LoadStep();
			
			//SceneManager.AddTemp(200,200);
		}
		
		private function AddMenuHandler(e:MouseEvent):void
		{
			var txt:TextField = e.target as TextField;			
			var countt:int = 0;
			var st:String = txt.text;
			if (st == "征战三国")
			{				
				for (var m:int = 0; m < 25; m++)
				{
				for (var n:int = 0; n < 38; n++)
				{
					var frams:Array = new Array();
				    for (var i:int = 0; i < 28; i++)
					{
				     	frams.push(ResourceManager.SoliderPool[n][i]);
						var dd:Dictionary = ResourceManager.SoliderPool;
				    }
					var x:int = Math.random() * 1100;//n % 20 * 100;
					var y:int = Math.random() * 1000;//int(n / 20) * 100;
				    var id:int = SceneManager.AddAnimationEnt(frams, x,y, 0, "AnimationEntity");
				    
				    var data:AnimationEntData = (AnimationEntData)(DataCenter.GetData(id));
			        
			        var animationUnit2:AnimationUnit = new AnimationUnit(DataSource.AnimationTest, 100);
					for (var f:int = 0; f < 28; f++)
					{
			          animationUnit2.AddFrameData(0, 0, f, 1);					  
					}
					
			        data.AddAnimationUnit(animationUnit2);
				    //MsgManager.RegistIDProc(id, DataSource.MsgMins["Update"], AnimationProc.Instance.Update, true);
					data.AddUpdateProc(AnimationProc.Instance.UpdateFrame,"UpdateFrame");					
					data.SwitchAnimation(DataSource.AnimationTest);
				}
			}			    
		}
				
			
			if (st == "存储进度")
			{
				SceneManager.AddOneSolider("Dhuta", 498, 234);
				//SceneManager.AddOneSolider("SpearManHorse-2", 400, 400);
				//SceneManager.AddOneSolider("Thief-2", 380, 600);
				//SceneManager.AddOneSolider("CrossbowManTank-2", 600, 300);
				//SceneManager.AddOneSolider("Catapult-3", 400, 500);
				
			}
		}
	}
}


                
				
				
				